/*
 * MainGameState.java
 *
 * Created on 10. M�rz 2007, 20:29
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.gamestate;

import com.jme.input.KeyInput;
import com.jme.input.controls.GameControl;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.binding.MouseAxisBinding;
import com.jme.input.controls.binding.MouseButtonBinding;
import com.jme.input.controls.controller.ActionController;
import com.jme.input.controls.controller.GameControlAction;
import com.jmex.game.state.BasicGameState;
import kanjitori.Main;
import kanjitori.Player;
import kanjitori.gamestate.controller.MoveController;
import kanjitori.gamestate.controller.TurnController;
import kanjitori.graphics.item.Item;
import kanjitori.preferences.KeyMap;
import kanjitori.util.SoundManager;

/**
 *
 * @author Pirx
 */
public class MainGameState extends BasicGameState {
    
    public final static String FORWARD = "forward";
    public final static String BACKWARD = "backward";
    public final static String STRAFE_LEFT = "strafeLeft";
    public final static String STRAFE_RIGHT = "strafeRight";
    public final static String TURN_LEFT = "turnLeft";
    public final static String TURN_RIGHT = "turnRight";
    public final static String TURN_UP = "turnUp";
    public final static String TURN_DOWN = "turnDown";
    
    public final static String TELEPORT = "teleport";
    public final static String DEF_CHANGE = "defChange";
    
    private final static String PAUSE = "pause";
    
    private final static float moveSpeed = 5;
    private final static float turnSpeed = 5;
    
    private boolean pause = false;
    
    
    /** Creates a new instance of MainGameState 
     * @param name 
     */
    public MainGameState(String name) {
        super(name);
    }
    
    public void initInput(KeyMap keyMap) {
        GameControlManager manager = new GameControlManager();
        
        
        manager.createControls(keyMap.ACTIONS);
        
        for (int k = 0; k < KeyMap.KEY_INPUT.length; k++) {
            String action = keyMap.getAction(KeyMap.KEY_INPUT[k]);
            if (action != null) {
                GameControl ctrl = manager.getControl(action);
                ctrl.addBinding(new KeyboardBinding(KeyMap.KEY_INPUT[k]));
            }
        }
        
        //MOVING
        GameControl forwardCtrl = manager.getControl(FORWARD);
        forwardCtrl.addBinding(new MouseButtonBinding(1));
        MoveController moveController = new MoveController(
                manager.getControl(FORWARD),
                manager.getControl(BACKWARD),
                manager.getControl(STRAFE_LEFT),
                manager.getControl(STRAFE_RIGHT),
                Player.getPlayer().getNode(),
                moveSpeed);
        getRootNode().addController(moveController);
        
        //TURNING
        GameControl upCtrl = manager.getControl(TURN_UP);
        upCtrl.addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_Y, keyMap.isMouseInverted()));
        GameControl downCtrl = manager.getControl(TURN_DOWN);
        downCtrl.addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_Y, ! keyMap.isMouseInverted()));
        GameControl leftCtrl = manager.getControl(TURN_LEFT);
        leftCtrl.addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_X, true));
        GameControl rightCtrl = manager.getControl(TURN_RIGHT);
        rightCtrl.addBinding(new MouseAxisBinding(MouseAxisBinding.AXIS_X, false));
        TurnController turnController = new TurnController(
                manager.getControl(TURN_LEFT),
                manager.getControl(TURN_RIGHT),
                manager.getControl(TURN_UP),
                manager.getControl(TURN_DOWN),
                Player.getPlayer().getCamNode(),
                turnSpeed);
        getRootNode().addController(turnController);
        
        //ITEMS
        GameControl teleCtrl = manager.getControl(TELEPORT);
        getRootNode().addController(new ActionController(teleCtrl, new GameControlAction() {
            public void pressed(GameControl gc, float time) {
                Player.getPlayer().useItem(Item.Type.TELEPORT);
            }
            public void released(GameControl gc, float time) {}            
        }));
        GameControl defChangeCtrl = manager.getControl(DEF_CHANGE);
        getRootNode().addController(new ActionController(defChangeCtrl, new GameControlAction() {
            public void pressed(GameControl gc, float time) {
                Player.getPlayer().useItem(Item.Type.DEF_CHANGE);
            }
            public void released(GameControl gc, float time) {}            
        }));
        
        //PAUSE
        GameControl pauseCtrl = manager.addControl(PAUSE);
        pauseCtrl.addBinding(new KeyboardBinding(KeyInput.KEY_ESCAPE));
        getRootNode().addController(new ActionController(pauseCtrl, new GameControlAction() {
            public void pressed(GameControl gc, float time) {
                Main.pauseGame();
            }
            public void released(GameControl gc, float time) {}
        }));
    }
    
    public void update(float tpf) {
        if (! pause) {
            super.update(tpf);
            SoundManager.getManager().update(tpf);
        }
    }
    
    public void setPause(boolean pause) {
        this.pause = pause;
    }
    
    public boolean isPause() {
        return pause;
    }
    
    public void render(float tpf) {
        super.render(tpf);
        SoundManager.getManager().draw();
    }
    
}
